package cn.kgc.springcloud.web.controller;

import cn.kgc.springcloud.web.entity.City;
import cn.kgc.springcloud.web.response.ResponseResult;
import cn.kgc.springcloud.web.service.CityService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @author 课工场
 * @date 2024/7/29
 * @description
 */
@RestController
@RequestMapping("city")
public class CityController {

    @Value("${server.port}")
    private String port;
    @Resource
    private CityService cityService;

    @GetMapping
    public ResponseResult cityList() {
        List<City> list = cityService.list();
        HashMap<String, Object> result = new HashMap<String, Object>();
        result.put("data",list);
        result.put("port",port);
        return ResponseResult.success().data(result);
    }


    @GetMapping("{id}")
    public City getOneCity(@PathVariable("id") Integer id) {
        City city = cityService.getById(id);
        return city;
    }

    @PostMapping
    public ResponseResult addCity(@RequestBody City city) {

        cityService.save(city);
        return ResponseResult.success().data(city);
    }
}
